<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category Action 
 * +------------------------------------------------------------+
 * 控制器基类
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2013
 * @version 1.0
 *
 * Modified at : 2013-2-16 16:21:47
 *
 */
abstract class Action extends Base {
	
	///模板引擎类对象
	protected $_view = null;
	
	///用于保存浏览器路径记录的各个步骤，每个元素包括标题和链接
	protected $_trail = array ();
	
	protected $_trail_string = '';
	
	///是否显示模板标识,true显示
	private $_show_view = true;
	
	protected $_page_title = '';
	
	//构造函数
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 
	 * +------------------------------------------------------------+
	 * @name assign
	 * +------------------------------------------------------------+
	 * 模板赋值，支持同时赋值多个
	 * +------------------------------------------------------------+
	 *
	 * @author anzai <236049524@qq.com>
	 * @version 1.0
	 *
	 * @example
	 *
	 * @param mixed $name
	 * @param mixed $value
	 *
	 */
	protected function assign($name, $value = ''){
		$this->_view()->assign($name, $value);
		
		return $this;
	}
	
	/**
	 * 自动显示模板
	 */
	public function _auto_display($filename='', $cache_id = 0, $suffix = ''){
		$this->display($filename, $cache_id, $suffix);
	}
	
	//编译并显示模板
	protected function display($filename='', $cache_id = 0, $suffix = ''){
		if ($this->_show_view) {
			C('AUTO_DISPLAY', false) != true OR $this->_setNoRender();
			$this->_preDisplay();
			$this->assign('TRAIL_DATAS', $this->_trail)
				->assign('TRAIL_STRING', $this->_trail_string)
				->assign('PAGE_TITLE', $this->_page_title);
			$this->_view()->display($filename, $cache_id, $suffix);
		}
	}
	
	//编译模板
	protected function fetch($filename='', $cache_id = 0, $suffix = ''){
		$this->_preDisplay();
		$this->assign('TRAIL_DATAS', $this->_trail)
				->assign('TRAIL_STRING', $this->_trail_string)
				->assign('PAGE_TITLE', $this->_page_title);
		return $this->_view()->fetch($filename, $cache_id, $suffix);
	}
	
	/**
	 * 
	 * +------------------------------------------------------------+
	 * @name _addStep
	 * +------------------------------------------------------------+
	 * 向浏览器路径增加一个步骤
	 * +------------------------------------------------------------+
	 *
	 * @author anzai <236049524@qq.com>
	 * @version 1.0
	 *
	 * @example
	 *
	 * @param string $title 标题
	 * @param string $url 关联url地址，可为空
	 * @return 对象本身
	 *
	 */
	protected function _addStep($title, $url=''){
		if (is_array($title)){
			if (isset($title['title'])){
				$this->_addStep($title['title'], $title['url']);
			}else{
				foreach ($title as $vo){
					$this->_addStep($vo['title'], $vo['url']);
				}
			}
		}elseif (!empty($title)){
			$key = md5(trim($title) . trim($url));
			if (!array_key_exists($key, $this->_trail)){
				$this->_page_title = $title;
				$this->_trail_string = empty($this->_title) ? $title : $title . '_' . $this->_title;
				$this->_trail[$key] = array('title'=>$title,'url'=>$url);
			}
		}
		return $this;
	}
	
	///解析模板前的相关操作
	protected function _preDisplay(){}
	
	/**
	 * 
	 * +------------------------------------------------------------+
	 * @name _sendJSON
	 * +------------------------------------------------------------+
	 * 将数组转换成  JSON 数据格式，并输出
	 * +------------------------------------------------------------+
	 *
	 * @author anzai <236049524@qq.com>
	 * @version 1.0
	 *
	 * @example
	 *
	 * @param mixed $data
	 *
	 */
	protected function _sendJSON($data){
		if ($data){
			if (!is_array($data)){
				exit($data);
			}else{
				header('Content-Type: application/json;charset=' . OUT_CHARSET);
				exit(json_encode($data));
			}
		}
	}
	
	///若开启自动显示模板功能后，在某一方法中不要显示模板时，可先调用该方法
	protected function _setNoRender(){
		$this->_show_view = false;
		return $this;
	}
	
	protected function _setShowRender(){
		$this->_show_view = true;
		return $this;
	}
	
	///获取模板引擎对象
	protected function _view(){
		if ($this->_view === null){
			$this->_view = Factory::view();
		}
		
		return $this->_view;
	}
	
	//PHP内置魔法函数，所调用方法不存在时，默认执行该方法
	public function __call($name, $arguments){
		E::halt(Helper::L('CLS_ACTION_EPT') . ' : ' . H(get_class($this) . '::' . $name . '()')) ;
	}
	
	//PHP内置魔法函数，用于快速模板赋值
	public function __set($name, $value){
		if (!property_exists($this, $name)){
			$this->name = $value;
			$this->assign($name, $value);
		}
	}
	
	//析构函数
	public function __destruct(){
		
	}
}

?>